﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Collections.Generic;
using Htss.Platform.Data;
namespace DreamBox.DAL
{
    public class GetPager
    {
        private int _pageSize = 0;
        /// <summary>
        /// 每页大小
        /// </summary>
        public int PageSize
        {
            get { return _pageSize; }
            set { _pageSize = value; }
        }
        public GetPager()
        {
            PageSize = PageSize == 0 ? 9 : PageSize;
        }
        DreamBox.DAL.Formpage page = new DreamBox.DAL.Formpage();
        /// <summary>
        /// 绑定分页数据
        /// </summary>
        /// <param name="p"></param>
        /// <param name="tablename"></param>
        /// <param name="colname"></param>
        /// <param name="where"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public DataTable GetFormPageData(int p, string tablename, string colname, string where, int order)
        {
            return page.Order(tablename, colname, PageSize, p, where, order);
        }
        public DbTable GetPageData(int p, string tablename, string colname, string where, int order)
        {
            return page.GetPageData(tablename, colname, PageSize, p, where, order);
        }
        public int GetTotalCount(string tablename, string colname, string where)
        {
            return int.Parse(page.Total(tablename, colname, PageSize, 1, where).Rows[0]["total"].ToString());
        }
        /// <summary>
        /// 生成分页信息
        /// </summary>
        /// <param name="p"></param>
        /// <param name="where"></param>
        /// <param name="tablename"></param>
        /// <param name="colname"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public List<string> GetPage(int p, string where, string tablename, string colname, string url)
        {
            List<string> list = new List<string>();
            string result = "";
            int RowTotal = GetTotalCount(tablename, colname, where);
            if (RowTotal > 0)
            {
                int PageCount = 0;
                //取得一共有多少页
                if (RowTotal % PageSize == 0)
                {
                    PageCount = RowTotal / PageSize;
                }
                else
                {
                    PageCount = RowTotal / PageSize + 1;
                }
                int frontcount = 1;
                int nextcount = 1;
                if (p > 1)
                {
                    frontcount = p - 1;
                }
                if (p == PageCount)
                {
                    nextcount = p;
                }
                else
                {
                    nextcount = p + 1;
                }
                result += "<div class=\"pagination\"><ul>";
                //result += "<li>共 " + RowTotal + "条记录 页码:" + p + "/" + PageCount;
                //result += " </li> <li><a href='" + url + "'class='first'>首页</a></li>";
                result += "<li class='prev'><a href='" + (url.Contains("?") == true ? (url + "&p=" + frontcount) : (url + "?p=" + frontcount)) + "'>上一页</a></li>";
                //数字分页
                if (p > 0 && p <= PageCount)
                {
                    if (p != 1)
                    {
                        result += "<li><a href='" + url + "' class='" + (p == 1 ? "" : "") + "'>1</a></li>";
                    }
                    else
                    {
                        result += "<li class='active'><a >1</a></li>";
                    }
                    //循环生成按钮
                    for (var i = 2; i < PageCount; i++)
                    {
                        if (p > 9)
                        {
                            if (i < (p - 4) && i > 1)
                            {
                                continue;
                            }
                        }
                        if (i > (p + 4) && i < PageCount)
                        {
                            break;
                        }
                        if (p == i)
                        {
                            result += "<li class='active'><a>" + i + "</a></li>";
                        }
                        else
                        {
                            result += "<li><a href='" + (url.Contains("?") == true ? (url + "&p=" + i) : (url + "?p=" + i)) + "'>" + i + "</a></li>";
                        }
                    }
                    if (PageCount != 1)
                    {
                        if (p == PageCount)
                        {
                            result += "<li class='active'><a>" + PageCount + "</a></li>";
                        }
                        else
                        {
                            result += "<li><a href='" + (url.Contains("?") == true ? (url + "&p=" + PageCount) : (url + "?p=" + PageCount)) + "'>" + PageCount + "</a></li>";
                        }
                    }
                }
                result += "<li class='next'><a href='" + (url.Contains("?") == true ? (url + "&p=" + nextcount) : (url + "?p=" + nextcount)) + "'>下一页</a></li></ul></div>";
                //result += "<a href='" + (url.Contains("?") == true ? (url + "&p=" + PageCount) : (url + "?p=" + PageCount)) + "'>末页</a>";
                list.Add(result);
            }
            else
            {
                list.Add("无数据记录!");
            }
            return list;
        }
    }
}